<template>
  <div class="layout" @click.stop="aotuTab=false">
    <div class="header">
      <jc-button
        :btn-arr="['reload', 'save', 'pass', 'list']"
        :disab-arr="['using', 'close', 'cancel', 'com1', 'com2', 'chart', 'freeze']"
        :list="'/otherOuterList'"
        @refresh="queryGetDetail1"
        @save="handleAdd"
        @audit="audit(1)"
        @noAudit="audit(0)"
      />

    </div>
    <el-tabs v-model="essential">
      <el-tab-pane :label="$t('market.info')" name="information">
        <el-form ref="purchaseRef" :model="optionValue" label-width="100px" size="mini">
          <el-form-item :label="$t('market.state')">
            <el-tag v-if="optionValue.fstatus === 1">{{ $t('market.state2') }}</el-tag>
            <el-tag v-else type="danger">{{ $t('market.state6') }}</el-tag>
          </el-form-item>
          <el-form-item :label="$t('market.billNo')">
            <el-input v-model="optionValue.fbillno" disabled />
          </el-form-item>
          <!--          <el-form-item label="库存组织">-->
          <!--            <el-select v-model.trim="optionValue.fstockorgid">-->
          <!--              <el-option-->
          <!--                v-for="option in teamList"-->
          <!--                :key="option.fid"-->
          <!--                :label="option.fname"-->
          <!--                :value="option.fid"-->
          <!--              />-->
          <!--            </el-select>-->
          <!--          </el-form-item>-->
          <!--          <el-form-item label="货主">-->
          <!--            <el-select v-model.trim="optionValue.fownerid" disabled>-->
          <!--              <el-option-->
          <!--                v-for="option in teamList"-->
          <!--                :key="option.fid"-->
          <!--                :label="option.fname"-->
          <!--                :value="option.fid"-->
          <!--              />-->
          <!--            </el-select>-->
          <!--          </el-form-item>-->
          <el-form-item :label="$t('market.note')" style="width: 50%;">
            <el-input v-model.trim="optionValue.fremark" style="width: 300px" :disabled="isDisabled" />
          </el-form-item>
        </el-form>
        <div>
          <div style="margin-bottom: 5px">
            <el-button size="mini" @click="twoLine(1)">{{ $t('btn.newHang') }}<!--新增行--></el-button>
            <el-button size="mini" @click="twoLine(2)">{{ $t('btn.delHang') }}<!--删除行--></el-button>
            <el-button size="mini" @click.native="batchFill">{{ $t('btn.batchFill') }}<!--批量填充--></el-button>
          </div>
          <jc-table
            :table-data="tableData"
            :table-header="tableHeader"
            :table-height="tableHeight"
            serial
            :get-summaries="getSummaries"
            show-summary
            @rowClick="rowClick"
            @cellClick="cellClick"
          >
            <!--  物料编码  -->
            <template v-slot:billSlot="col">
              <el-input
                v-model.trim="col.scope.row.fmaterialfnumber"
                size="mini"
                :disabled="isDisabled"
                @input="getAtuo(col.scope.$index, col.scope.row.fmaterialfnumber)"
              >
                <i
                  slot="suffix"
                  class="el-input__icon el-icon-search"
                  @click="handleGetMateriel(col.scope.row, col.scope.$index)"
                />
              </el-input>
              <div v-if="aotuTab && indexSelf === col.scope.$index" :class="indexSelf<7 ? 'tabPosit': 'tabPosit1'">
                <number-code :fnumber="fnumber" @material="materialData" />
              </div>
            </template>
            <!--  数量  -->
            <template v-slot:tagSlot="col">
              <el-input-number v-model="col.scope.row.fqty" size="mini" :disabled="isDisabled" />
            </template>
            <!--  库位  -->
            <template v-slot:btnState="col">
              <el-select v-model="col.scope.row.fcodefnumber" size="mini" :disabled="isDisabled">
                <el-option v-for="ite in col.scope.row.codes" :key="ite.fid" :label="ite.positionfnumber" :value="ite.fnumber" />
              </el-select>
            </template>
          </jc-table>
        </div>
      </el-tab-pane>
    </el-tabs>
    <!--物料弹窗-->
    <material v-if="isMateria" @material="materialData" />
  </div>
</template>

<script>
import autocomplete from '@/views/market/marketManage/createMarkerOrder/components/autocomplete'
import bility from '@/mixin/Operability'
import { getTBdCodeListt, queryDetails1, updateOutstock, updateStatus1 } from '@/api/wms/stock'
import jcButton from '@/components/Button'

export default {
  name: 'OtherWarehousingAdd',
  components: {
    jcButton,
    material: () => import('@/views/market/marketManage/createMarkerOrder/components/material'),
    numberCode: () => import('@/components/NumberCode')
  },
  mixins: [autocomplete, bility],
  data() {
    return {
      essential: 'information',
      fnumber: '',
      isMateria: false,
      aotuTab: false,
      isDisabled: false,
      // 表头数据
      tableData: [],
      tableHeader: [
        { label: this.$t('market.fmaterial'), type: 'bill', prop: 'fmaterialfnumber', width: '150px' },
        { label: this.$t('market.fmodel'), prop: 'fmodel' },
        { label: this.$t('shop.fname2'), prop: 'fmaterialfname' },
        { label: this.$t('shop.fspecificaTion'), prop: 'fspecification' },
        { label: this.$t('market.funit'), prop: 'unit', width: '80' },
        { label: this.$t('market.fqty'), type: 'tag', prop: 'fqty', width: '80' },
        { label: this.$t('market.storageLoca'), type: 'state', prop: 'fcodefnumber', width: '150' },
        { label: this.$t('market.fdescripTion'), prop: 'fdescripTion', minWidth: '200px' }
      ],
      optionValue: {},
      tableHeight: '',
      indexSelf: 0
    }
  },
  created() {
    // if (this.$route.params.id) {
    this.queryGetDetail1()
    // }
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 280px)' : '400px'
  },
  methods: {
    queryGetDetail1() {
      const fid = this.$route.params.id
      queryDetails1({ fid }).then(res => {
        if (res.code === 0) {
          const { fbillno, fremark, fstatus } = res.data[0]
          this.optionValue = {
            fbillno, fremark, fstatus, fid
          }
          this.tableData = res.data
          this.isDisabled = fstatus === 1
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 输入物料编码
    getAtuo(index, fnumber) {
      this.indexSelf = index
      this.aotuTab = true
      var vm = this
      clearTimeout(vm.timer) // 清除未执行的代码，重置回初始化状态
      vm.timer = setTimeout(() => {
        this.fnumber = fnumber
      }, 600)
    },
    // 保存
    handleAdd() {
      this.details = []
      const data = {
        ...this.optionValue,
        entitys: this.tableData
      }
      updateOutstock(data).then(res => {
        const { code, message } = res
        if (code !== 0) {
          this.$confirm(message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
          return
        }
        this.queryGetDetail1()
        this.$message({ showClose: true, message: res.message, type: 'success' })
      })
    },
    // 打开物料弹窗
    handleGetMateriel(row, index) {
      this.isMateria = true
      this.indexSelf = index
    },
    // 物料数据
    materialData(item) {
      if (item.fmaterialId) {
        const items = this.tableData[this.indexSelf]
        items.fmaterialid = item.fmaterialId
        items.fmaterialfnumber = item.fnumber
        items.fmaterialfname = item.fname
        items.fdescripTion = item.fdescripTion
        items.fspecification = item.fspecificaTion
        items.funitid = item.funitId
        items.unit = item.funit
        items.fmodel = item.fmodel
        this.queryTBdCode(item.fmaterialId)
      }
      this.isMateria = false
    },
    queryTBdCode(ev) {
      const items = this.tableData[this.indexSelf]
      const DATA = {
        fmaterialid: ev
      }
      getTBdCodeListt(DATA).then(res => {
        if (res.code === 0) {
          items.codes = res.data
          items.fcodefnumber = res.data[0].fnumber
        }
      })
    },
    // 单击行
    rowClick(row) {
      this.indexSelf = row.index
    },
    // 新增 删除行
    twoLine(ev) {
      const data = {
        fmaterialfnumber: '',
        fmodel: '',
        fspecificaTion: '',
        fdescripTion: '',
        funitName: '',
        fqty: 0,
        fentryid: 0,
        fcodefnumber: '',
        codes: []
      }
      if (ev === 1) {
        this.tableData.push(data)
      } else {
        this.tableData.splice(this.indexSelf, 1)
      }
    },
    // 点击单元格
    cellClick(row, column, cell, event) {
      this.property = column.property
      this.subscript = row.index
    },
    // 审核
    audit(ev) {
      const DATA = {
        numbers: [this.optionValue.fbillno],
        fstatus: ev
      }
      updateStatus1(DATA).then(res => {
        if (res.code === 0) {
          this.queryGetDetail1()
          this.$message({ showClose: true, message: res.message, type: 'success' })
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 批量填充
    batchFill() {
      const item = this.tableData[this.subscript][this.property]
      this.tableData.map((res, index) => {
        if (index >= this.subscript) {
          res[this.property] = item
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.layout {
  @include listBom;
  padding: 10px;
  background: #FFFFFF;
  .tabPosit {
    padding: 5px;
    box-shadow: 0 0 2px #aaa;
    position: absolute;
    top: 3.5vh;
    left: 0;
    background-color: rgba(255, 255, 255, 1);
    z-index: 99999!important;
  }
  .tabPosit1 {
    padding: 5px;
    box-shadow: 0 0 2px #aaa;
    position: absolute;
    top: -25.69vh;
    left: 0;
    background-color: rgba(255, 255, 255, 1);
    z-index: 9999;
  }
  .el-input::v-deep .el-input__inner {
    height: 30px;
    line-height: 30px;
  }
  .el-form {
    display: flex;
    flex-wrap: wrap;

    .el-form-item {
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      //height: 35px;
      width: 275px;
      margin-bottom: 5px;
      .btnType{
        margin-right: 20px;
      }
      .el-date-editor.el-input{
        width: 100% !important;
      }
      .el-select{
        width: 100%;
      }
    }
  }
}
</style>
